<?php

class pay_core{
	private $_acceptedLanguajes;
	private $_languajeDefault=array("primitive"=>"es",	"variant"=>"");
	var $lang="es";
	var $langVariant="";
	var $lang_user_defined=false;
	var $browser;
	var $user;
	var $dbs=array();

	public function setUserLang($lang="es",$variant=""){
		setcookie("lang","",time()-1);
		unset($_COOKIE["lang"]);
		$this->defLanguaje($lang,$variant);
		setcookie("lang",$this->lang."_".$this->langVariant,null,"/");
		$this->lang_user_defined=true;
	}
	public function loadClass($name){
		global $config;
		if(file_exists($config->paths["core/class"].$name.'.class.php')){
			include_once $config->paths["core/class"].$name.'.class.php';
			if(class_exists($name)){
				return $name;
			}
		}
		return false;
	}
	

/**
 * Detecta y define el lenguaje a utilizar por el nucleo.
 * @param STRING (optional) $defaultPrimitive = "es"
 * @param STRING (optional) $defaultVariant = ""
 */
	public function defLanguaje($defaultPrimitive="",$defaultVariant=""){
		global $config;
		  //print_r($_COOKIE);
		  //exit;
		if(array_key_exists("lang", $_COOKIE)){
		  $lang=explode("_",$_COOKIE["lang"]);

		  $this->lang=$lang[0];
		  $this->langVariant=$lang[1];
		  $this->lang_user_defined=true;
		}
		else {
		 $dp=$config->paths["core/languajes"]."/#DP#/#DP#.lang.php";
		  $dp=str_replace("#DP#",$defaultPrimitive,$dp);
		 $dv=$config->paths["core/languajes"]."/#DP#/#DP#_#DV#.lang.php";
		  $dv=str_replace(array("#DP#","#DV#"),array($defaultPrimitive,$defaultVariant),$dv);
		  if(file_exists($dp)){
		    $this->lang=$defaultPrimitive;
		    $languaje_Found=true;
		    if(file_exists($dv)){
			$this->langVariant=$defaultVariant;
		      }
		    }
		  else {
		    $this->_adivinaLenguaje();
		  }
		}
	}
	private function _adivinaLenguaje(){

		global $config;
		$languajes=explode(";",$_SERVER["HTTP_ACCEPT_LANGUAGE"]);
		$this->_acceptedLanguajes=array();
		foreach($languajes as $l){
			$l=explode(",",$l);
			$this->_acceptedLanguajes[$l[count($l)-1]]=array("primitive"=>$l[count($l)-1],
										"variant"=>$l[0]);
		}
		foreach($this->_acceptedLanguajes as $l){
			  if(file_exists($config->paths["core/languajes"].$l["primitive"])){
				  $this->_languajeDefault=$l;
				  $languaje_Found=true;
				  break;
			  }
		  }
		  $this->lang=$this->_languajeDefault["primitive"];
		  $this->langVariant=$this->_languajeDefault["variant"];
		  $this->lang_user_defined=false;

	}
	public function getLexicon(){
		global $config;
		 $dp=$config->paths["core/languajes"]."/#DP#/#DP#.lang.php";
		  $dp=str_replace("#DP#",$this->lang,$dp);

		 $dv=$config->paths["core/languajes"]."/#DP#/#DP#_#DV#.lang.php";
		  $dv=str_replace(array("#DP#","#DV#"),array($this->lang,$this->langVariant),$dv);

		    if(file_exists($dv)){
			$langpath=$dv;
			include_once $langpath;
		      }
		    elseif(file_exists($dp)){
			$langpath=$dp;
			include_once $langpath;
		    }
	    if(class_exists("languaje")){
	      include_once $config->paths["core/class"].'lexicon.class.php';
	      $d=new languaje();
	      $l=new lexicon($d);
	      return $l;
	    }
	}
	/**
	 * 
	 * Detecta y almacena las características del navegador
	 */
	public function defBrowser(){
		$this->browser=get_browser();
	}
	/**
	 * 
	 * Detecta y autentifica al usuario
	 * TODO: Agregar métodos de autentificación
	 */
	public function defUser(){
		global $config,$user;
		if(!session_id()){
			session_start();
			}
		include_once $config->paths["core/class"].'user.class.php';
		$user=new user();
		$user->loginWithCookie(md5($config->defaults["secretword"]));
		$this->user=&$user;
	}
	public function defDB(){
		global $config;
		include_once $config->paths["core/class"].'db.class.php';
		$dbs=array();
		foreach($config->db as $db){
			$dbs[]=new db($db);
		}
		$this->dbs=$dbs;
	}
	public function getDB($index=0){
	  return $this->dbs[$index];
	}

	public function getDocument($doc){
	  global $config;
	  if(file_exists($config->paths["html"].$doc)){
	    include_once $config->paths["core/class"].'document.class.php';
	    $document=new document($config->paths["html"].$doc);
	    return $document;
	  }
	}
  public function getChunk($key,$params=array()){
	  global $config;

    $keys=trim($key,"[]");
	$keys=explode("?",$keys);
	$key=$keys[0];
	if($keys[1]){
		$params=is_array($params)?$params:array();
		$keys=explode("&",$keys[1]);
		foreach($keys as $k){
			$k=explode("=",$k);
			$params[$k[0]]=trim($k[1],"`");
		}
	}
    if(file_exists($config->paths["chunks"].$key.'.chunk.php')){
      include_once $config->paths["chunks"].$key.'.chunk.php';
      $k=$key."_chunk";
      if(class_exists($k)){
	    
	  $chunk=new $k($params);
	  return $chunk;
	}
      }
  }
  public function getApp($appName,$params=false){
    global $config;
    $app=null;
    if(file_exists($config->paths["app"].$appName."/".$appName.".app.php")){
      include_once $config->paths["app"].$appName."/".$appName.".app.php";
      $appName=$appName."_app";
      if(class_exists($appName)){
	$app=new $appName($params);
      }
    }
    return $app;
  }
  public function getHandler($name,$params=false){
    global $config;
	include_once $config->paths["core/interfaces"]."/handler.php";
  	$handler=null;
    if(file_exists($config->paths["handlers"].$name."/".$name.".handler.php")){
      include_once $config->paths["handlers"].$name."/".$name.".handler.php";
      $name=$name."_handler";
	  $handler=new $name($params);
	}
	return $handler;
  }
  public function addAlert($message,$level=5){
    $_SESSION["alerts"][]=array("message"=>$message,"level"=>$level);
  }
  public function getAlerts(){
    $alerts=$_SESSION["alerts"];
    unset($_SESSION["alerts"]);
    return $alerts;
  }
  
}